package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.PageDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Employee;
import com.sky.entity.Setmeal;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.query.CategoryQuery;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author 马毅爽
* @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
* @createDate 2023-11-03 21:00:56
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void save(CategoryDTO categoryDTO) {
        Category category = BeanUtil.copyProperties(categoryDTO,Category.class);
        category.setStatus(StatusConstant.ENABLE);

        save(category);
    }

    @Override
    public PageDTO<Category> queryPage(CategoryQuery categoryQuery) {
        String name = categoryQuery.getName();
        Integer type = categoryQuery.getType();

        Page<Category> page = categoryQuery.sortBy("sort",false);

        Page<Category> p = lambdaQuery()
                .like(name != null,Category::getName,name)
                .eq(type != null,Category::getType,type)
                .page(page);

        return PageDTO.of(p);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Category category = Category.builder()
                .status(status)
                .id(id)
                .build();
        updateById(category);
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = BeanUtil.copyProperties(categoryDTO,Category.class);
        updateById(category);
    }

    @Override
    public void deleteById(Long id) {
        Long count = dishMapper.selectCount(new LambdaQueryWrapper<Dish>().eq(Dish::getCategoryId,id));

        if (count > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        count = setmealMapper.selectCount(new LambdaQueryWrapper<Setmeal>().eq(Setmeal::getCategoryId,id));

        if (count > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        removeById(id);
    }

    @Override
    public List<Category> queryType(Integer type) {
        return lambdaQuery().eq(type != null,Category::getType,type).orderByDesc(Category::getSort).list();
    }
}




